package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /*
    * 新增菜品
    * */
    @Override
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 向菜品表插入一条数据
        dishMapper.insertDish(dish);

        // 向口味表插入多条数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();

        if (dishFlavors != null && !dishFlavors.isEmpty()) {
            // 遍历口味集合，为每个口味对象设置菜品id
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            // 调用插入方法
            dishMapper.insertDishFlavor(dishFlavors);
        }

    }

    /*
    * 菜品分页查询
    * */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        // 设置分页参数，页码和每页记录数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        // 调用查询方法，PageHelper会自动将分页信息封装到Page对象中，并返回
        Page<Dish> dishList = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(dishList.getTotal(), dishList.getResult());
    }

    /*
    * 根据口味查询菜品
    * */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        // 根据id查询菜品数据
        DishVO dishVO = dishMapper.selectById(id);
        // 根据id查询口味数据
        List<DishFlavor> dishFlavors = dishMapper.selectByIdWithFlavor(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /*
    * 根据菜品id删除菜品
    * */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        // 判断当前菜品是否在起售中
        for (Long id : ids) {
            DishVO dishVO = dishMapper.selectById(id);
            if (dishVO.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 判断当前菜品是否关联套餐
        if (setmealDishMapper.countByDishId(ids) > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除菜品数据
        dishMapper.deleteByIds(ids);
        // 删除口味数据
        for (Long id : ids){
            dishMapper.deleteById(id);
        }
    }

    /*
    * 根据id修改状态
    * */
    @Override
    public void UpdateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    /*
    * 修改菜品信息
    * */
    @Override
    @Transactional
    public void update(@RequestBody DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 修改菜品表的信息
        dishMapper.update(dish);

        // 删除原有的口味数据
        dishMapper.deleteById(dishDTO.getId());

        // 插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()){
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishMapper.insertDishFlavor(flavors);
        }
    }

    /*
    * 根据分类id查询菜品
    * */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        // 获取到菜品数据
        List<Dish> DishList = dishMapper.list(dish);
        // 创建一个列表
        List<DishVO> dishVOList = new ArrayList<>();
        // 循环将口味数据插入到dishVOList重对应的DishVO
        for (Dish d : DishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(d.getId());
            dishVO.setCategoryName(categoryMapper.selectCateNameById(d.getCategoryId()));
            dishVO.setFlavors(dishFlavorList);
            dishVOList.add(dishVO);
        }
       return dishVOList;
    }
}
